//
//  PlayScreenViewController.m
//  TravelEveryDay
//
//  Created by 路洪宇 on 15/10/19.
//  Copyright © 2015年 com.lanou3g. All rights reserved.
//

#import "PlayScreenViewController.h"
#import "Play_CollectionViewCell.h"
#import "BaseHeader.h"
@interface PlayScreenViewController ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UITableViewDataSource,UITableViewDelegate>

@end

@implementation PlayScreenViewController

-(void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];

#pragma mark - 获取本地标签数组
    NSString *home = NSHomeDirectory();
    NSString * docpath = [home stringByAppendingPathComponent:@"Documents"];
    NSString * filepath = [docpath stringByAppendingPathComponent:@"data.plist"];
    NSArray *tempArr = [NSArray arrayWithContentsOfFile:filepath];
    _play_passArr = [NSMutableArray arrayWithArray:tempArr];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = TITLECOLOR;
    _play_passArr = [[NSMutableArray alloc] init];
    _play_chooseArr = [[NSMutableArray alloc] init];
    _play_markArray = @[@"全部",@"亲子",@"文化体验",@"周边游",@"旅行资讯",@"美食",@"城市漫步",@"DIY",@"艺术",@"运动",@"聚会&派对",@"户外活动",@"Brunch&下午茶",@"社交",@"摄影"];
    _play_sortArray = @[@"智能排序",@"人气最高"];
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setImage:[UIImage imageNamed:@"play-close.png"] forState:UIControlStateNormal];
    backButton.frame = CGRectMake(SCREEN_WIDTH/2-12.5, SCREEN_HEIGHT-80*HEIGHT, 25, 25);
    [backButton addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButton];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2-100, 40*HEIGHT, 200, 30)];
    [titleLabel setText:@"筛选&排序"];
    [titleLabel setFont:[UIFont systemFontOfSize:18.]];
    [titleLabel setTextColor:[UIColor whiteColor]];
    [titleLabel setTextAlignment:NSTextAlignmentCenter];
    [self.view addSubview:titleLabel];
    
    UIButton *saveButton = [UIButton buttonWithType:UIButtonTypeCustom];
    saveButton.frame = CGRectMake(SCREEN_WIDTH/2-(150*WIDTH)/2, backButton.frame.origin.y-70*HEIGHT,150*WIDTH, 40*HEIGHT);
    [saveButton addTarget:self action:@selector(saveClick) forControlEvents:
     UIControlEventTouchUpInside];
    [saveButton setTitle:@"保存" forState:UIControlStateNormal];
    saveButton.titleLabel.font = [UIFont systemFontOfSize:17.];
    saveButton.layer.cornerRadius = 17;
    saveButton.layer.borderWidth = .5;
    saveButton.layer.masksToBounds = YES;
    saveButton.layer.borderColor = [[UIColor whiteColor]CGColor ];
    [self.view addSubview:saveButton];
    [self setUpTableView];
    [self SetUpCollectionView];
}


-(void)SetUpCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    flowLayout.itemSize = CGSizeMake((SCREEN_WIDTH-30-20)/3, 35*HEIGHT);
    //系统默认为10
    flowLayout.minimumInteritemSpacing = 15;
    flowLayout.minimumLineSpacing = 10;
    
    flowLayout.sectionInset = UIEdgeInsetsMake(40, 10, 50, 10);//上左下右(对整个section外边框)
    
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 100*HEIGHT, SCREEN_WIDTH, 310*HEIGHT)collectionViewLayout:flowLayout];
    
    _collectionView.backgroundColor = TITLECOLOR;
    //设置多选
    _collectionView.allowsMultipleSelection = YES;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    UILabel *choiseLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 30)];
    [choiseLabel setText:@"选择标签"];
    [choiseLabel setTextColor:[UIColor whiteColor]];
    [choiseLabel setTextAlignment:NSTextAlignmentLeft];
    [choiseLabel setFont:[UIFont systemFontOfSize:15.]];
    choiseLabel.alpha = 0.7;
    [_collectionView addSubview:choiseLabel];
    
    UILabel *sortLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 280*HEIGHT, 100, 20)];
    [sortLabel setText:@"排序"];
    [sortLabel setTextColor:[UIColor whiteColor]];
    [sortLabel setTextAlignment:NSTextAlignmentLeft];
    [sortLabel setFont:[UIFont systemFontOfSize:15.]];
    sortLabel.alpha = 0.7;
    [_collectionView addSubview:sortLabel];
    
    [_collectionView registerClass:[Play_CollectionViewCell class] forCellWithReuseIdentifier:@"Play_CollectionViewCell"];
    self.play_SortTableView.tableHeaderView =  _collectionView;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 15;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    Play_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Play_CollectionViewCell" forIndexPath:indexPath];

    //读取沙盒文件
    NSString *home = NSHomeDirectory();
    NSString * docpath = [home stringByAppendingPathComponent:@"Documents"];
    NSString * filepath = [docpath stringByAppendingPathComponent:@"data.plist"];
    _play_chooseArr = [NSArray arrayWithContentsOfFile:filepath];
    [cell.circleLabel setText:[self.play_markArray objectAtIndex:indexPath.row]];

#pragma mark - 遍历本地标签数组如果有对应标签将标签改为对应选中状态
    for (NSString *string in _play_chooseArr) {
        if ([cell.circleLabel.text isEqualToString:string]) {
            cell.circleLabel.backgroundColor = [UIColor whiteColor];
            cell.circleLabel.textColor = TITLECOLOR;
        }
    }
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    Play_CollectionViewCell *cell = (Play_CollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    
#pragma mark - 判断如果是点击状态,再点击从写入本地标签数组中移除对应标签
    if (cell.circleLabel.backgroundColor == [UIColor whiteColor]) {
        
        [_play_passArr removeObject:[self.play_markArray objectAtIndex:indexPath.row]];
        
        cell.circleLabel.backgroundColor = TITLECOLOR;
        cell.circleLabel.textColor = [UIColor whiteColor];

    }else{
        [_play_passArr addObject:[self.play_markArray objectAtIndex:indexPath.row]];
        cell.circleLabel.backgroundColor = [UIColor whiteColor];
        cell.circleLabel.textColor = TITLECOLOR;
    }
    NSLog(@"沙盒数组:%@",_play_passArr);
}


#pragma mark - Set Up TableView
-(void)setUpTableView
{
    _play_SortTableView  = [[UITableView alloc]initWithFrame:CGRectMake(0, 100*HEIGHT, SCREEN_WIDTH, 350*HEIGHT) style:UITableViewStylePlain];
    _play_SortTableView.backgroundColor = TITLECOLOR;
    _play_SortTableView.delegate = self;
    _play_SortTableView.dataSource = self;
    [_play_SortTableView setSeparatorColor:[UIColor whiteColor]];
    [self.view addSubview:_play_SortTableView];
}

#pragma mark- TableView Delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40*HEIGHT;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"UITableViewCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellId];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = TITLECOLOR;
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.textLabel.text = [_play_sortArray objectAtIndex:indexPath.row];
    [cell.textLabel setFont:[UIFont systemFontOfSize:15.]];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.delegate giveSort:[_play_sortArray objectAtIndex:indexPath.row]];
    [self dismissViewControllerAnimated:YES completion:^{
    }];
}


-(void)backClick
{
    [self dismissViewControllerAnimated:YES completion:^{
    }];
}

#pragma mark 保存点击方法(将标签数组写入本地)
-(void)saveClick
{
    [self.delegate giveScreen:_play_passArr];
    
    NSString *home = NSHomeDirectory();
    NSString * docpath = [home stringByAppendingPathComponent:@"Documents"];
    NSString * filepath = [docpath stringByAppendingPathComponent:@"data.plist"];
    [_play_passArr writeToFile:filepath atomically:YES];
    NSLog(@"%@",_play_passArr);
    
    
    [self dismissViewControllerAnimated:YES completion:^{
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
